package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.sqfw.models.Sqfw_completeflow;
import com.budwk.app.sqfw.models.Sqfw_workorder;
import com.budwk.app.sqfw.services.SqfwTextReplaceService;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.sqfw.models.Sqfw_ImgCallback;
import com.budwk.app.sqfw.services.SqfwImgcallbackService;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/sqfw/imgCallback")
@ApiDefinition(tag = "图片识别回调")
@Slf4j
public class SqfwImgCallbackController {
    @Inject
    private SqfwImgcallbackService sqfwImgcallbackService;
    @Inject
    private SqfwTextReplaceService sqfwTextReplaceService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询图片识别回调")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = Sqfw_ImgCallback.class
    )
    @ApiResponses(
            implementation = Sqfw_ImgCallback.class
    )
    @SaCheckPermission("sqfw.imgcallback")
    @SLog("分页查询图片识别回调")
    public Result<?> list(@Param("..") Sqfw_ImgCallback bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getCode() != null && Strings.isNotBlank(bean.getCode().toString())) {
            cnd.and("code", "=", bean.getCode());
        }

        if (bean.getMessage() != null && Strings.isNotBlank(bean.getMessage().toString())) {
            cnd.and("message", "=", bean.getMessage());
        }

        if (bean.getRequestId() != null && Strings.isNotBlank(bean.getRequestId().toString())) {
            cnd.and("requestId", "=", bean.getRequestId());
        }

        if (bean.getAppId() != null && Strings.isNotBlank(bean.getAppId().toString())) {
            cnd.and("appid", "=", bean.getAppId());
        }

        if (bean.getProjectId() != null && Strings.isNotBlank(bean.getProjectId().toString())) {
            cnd.and("projectId", "=", bean.getProjectId());
        }

        if (bean.getImgUrl() != null && Strings.isNotBlank(bean.getImgUrl().toString())) {
            cnd.and("imgUrl", "=", bean.getImgUrl());
        }

        if (bean.getText() != null && Strings.isNotBlank(bean.getText().toString())) {
            cnd.and("text", "=", bean.getText());
        }

        if (bean.getResultDetail() != null && Strings.isNotBlank(bean.getResultDetail().toString())) {
            cnd.and("resultDetail", "=", bean.getResultDetail());
        }

        if (bean.getTakeTime() != null && Strings.isNotBlank(bean.getTakeTime().toString())) {
            cnd.and("takeTime", "=", bean.getTakeTime());
        }

        if (bean.getBusinessType() != null && Strings.isNotBlank(bean.getBusinessType().toString())) {
            cnd.and("businessType", "=", bean.getBusinessType());
        }

        if (bean.getBusinessId() != null && Strings.isNotBlank(bean.getBusinessId().toString())) {
            cnd.and("businessId", "=", bean.getBusinessId());
        }

        return Result.data(sqfwImgcallbackService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增图片识别回调")
    @ApiFormParams(
            implementation = Sqfw_ImgCallback.class
    )
    @ApiResponses
    @SLog(tag = "新增图片识别回调", value = "${args[0]}")
    @SaCheckPermission("sqfw.imgcallback.create")
    public Result<?> create(@Param("..") Sqfw_ImgCallback sqfwImgcallback, HttpServletRequest req) {
        sqfwImgcallback.setCreatedBy(SecurityUtil.getUserId());
        sqfwImgcallbackService.insert(sqfwImgcallback);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改图片识别回调")
    @ApiFormParams(
            implementation = Sqfw_ImgCallback.class
    )
    @ApiResponses
    @SLog(tag = "修改图片识别回调", value = "${args[0]}")
    @SaCheckPermission("sqfw.imgcallback.update")
    public Result<?> update(@Param("..") Sqfw_ImgCallback sqfwImgcallback, HttpServletRequest req) {
        sqfwImgcallback.setUpdatedBy(SecurityUtil.getUserId());
        sqfwImgcallbackService.updateIgnoreNull(sqfwImgcallback);
        return Result.success();
    }

    @At("/updateBusId")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改busid")
    @ApiFormParams(
            implementation = Sqfw_completeflow.class
    )
    @ApiResponses
    @SaCheckLogin
    @SLog(tag = "分页查询图片识别回调", value = "${args[0]},${args[0]}")
    public Result<?> updateBusId(@Param("imgId") String imgId,@Param("busId") String busId) {
        if (StringUtils.isNotBlank(imgId)){
            Sqfw_ImgCallback sqfw_imgCallback=sqfwImgcallbackService.fetch(imgId);
            sqfw_imgCallback.setBusinessId(busId);
            sqfwImgcallbackService.updateIgnoreNull(sqfw_imgCallback);
        }
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取图片识别回调")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckPermission("sqfw.imgcallback")
    @SLog(tag = "获取图片识别回调", value = "${args[0]}")
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        Sqfw_ImgCallback sqfwImgcallback = sqfwImgcallbackService.fetch(id);
        if (sqfwImgcallback == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sqfwImgcallback);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除图片识别回调")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除图片识别回调", value = "${args[0]}")
    @SaCheckPermission("sqfw.imgcallback.delete")
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        Sqfw_ImgCallback sqfwImgcallback = sqfwImgcallbackService.fetch(id);
        if (sqfwImgcallback == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwImgcallbackService.delete(id);
        req.setAttribute("_slog_msg", sqfwImgcallback.getId());
        return Result.success();
    }


    @Ok("json")
    @ApiOperation(name = "光哥专用图片识别回调")
    @POST
    @At("/imgcallback")
    @SLog(tag = "光哥专用图片识别回调", value = "${args[0]},${args[1]}")
    public Result<?>  imgcallback(@Param("imgUrl") String imgUrl,@Param("text") String text) {
        String newContentStr = sqfwTextReplaceService.replaceText(text, "img");
        Sqfw_ImgCallback sqfwImgcallback  = new Sqfw_ImgCallback();
        sqfwImgcallback.setImgUrl(imgUrl);
        sqfwImgcallback.setResultDetail(text);
        sqfwImgcallback.setText(newContentStr);
        sqfwImgcallback.setBusinessType("douyin");//类型抖音
        sqfwImgcallbackService.insert(sqfwImgcallback);
        return Result.data(newContentStr) ;
    }
}
